#!/usr/bin/python2.7

import Queue
import time

queue_size = 5
total_items = 10
concurrent_limit = 2

def task(name):
    print "Running task "  + name
    time.sleep(5)

def queue_info(queue):
    if queue.empty():
        print "my_queue is empty"
    else:
        print "my_queue size = ", queue.qsize()

total_counter = 1;
concurrent_counter = 0;
my_queue = Queue.Queue(maxsize=queue_size)
queue_status = 'start'

while queue_status == 'start':
    if my_queue.qsize() > 0: 
        if concurrent_counter < concurrent_limit + 1:
            concurrent_limit+=1
            current_process = my_queue.get()
            task(current_process)
        else:
            concurrent_counter = 0;
    else:
        if total_counter < total_items + 1:
            my_queue.put("test" + str(total_counter))
            total_counter += 1
        elif total_counter == total_items + 1 and my_queue.qsize() == 0: 
            queue_status = 'stop'

queue_info(my_queue)
